layui.define(['table', 'form', 'upload', 'admin'], function(exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    /* ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————— */

    table.render({
        elem: '#LAY-goods-manage'
        , url: '/admin/shop/getGoodsList'
        , cols: [[
            {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'title', width: 200, title: '商品名'}
            , {field: '', title: '轮播图片', width: 100, templet: function (d) {
                    if (d.thumb_urls.length>0){
                        var images = d.thumb_urls.split(",");
                        return '<img style="display: inline-block; width: auto; height: 100%;" src= "'+ images[0] +'" layadmin-event="photosPreview" value="'+d.thumb_urls+'">'
                    }else {
                        return "暂未上传";
                    }
                }}
            , {field: '', title: '详情图片', width: 100, templet: function (d) {
                    if (d.info_urls.length>0){
                        var images = d.info_urls.split(",");
                        return '<img style="display: inline-block; width: auto; height: 100%;" src= "'+ images[0] +'" layadmin-event="photosPreview" value="'+d.info_urls+'">'
                    }else {
                        return "暂未上传";
                    }
                }}
            , {field: '', title: '分类', width: 100, templet: function (d) {
                    return d.category.title;
                }}
            , {field: 'price', title: '价格', width: 110}
            , {field: 'stock', title: '库存', width: 110}
            , {field: 'limit_count', title: '日限购', width: 110}
            , {field: 'create_time', minWidth: 170, title: '发布时间'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-goodslist-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-goods-manage)', function(obj){
        if(obj.event === 'del'){
            layer.confirm('确定要删除该商品？', function(index){
                admin.req({
                    url: '/admin/shop/goods_del_post'
                    ,data: {"id":obj.data.id}
                    ,success(res){
                        if (res.code == 0){
                            table.reload("LAY-goods-manage");
                            layer.msg(res.msg, {icon:1});
                        } else{
                            layer.msg(res.msg, {icon:5});
                        }
                    }
                });
            });
        } else if(obj.event === 'edit'){
            var index = layer.open({
                type: 2
                ,title: '编辑'
                ,content: '/admin/shop/goods_edit?id='+obj.data.id
                ,maxmin: true
                ,area: ['650px', '350px']
            });
            layer.full(index);
        }
    });

    function setGoodsStatus(goodsid,status){
        admin.req({
            url: '/admin/shop/setGoodsStatus_post'
            ,data: {"id":goodsid,"status":status}
            ,success(res){
                if (res.code == 0){
                    table.reload("LAY-goods-manage");
                    layer.msg(res.msg, {icon:1});
                } else{
                    layer.msg(res.msg, {icon:5});
                }
            }
        });
    }

    //查看照片
    admin.events.photosPreview = function(othis){
        let urls = othis.attr("value");
        let urlarr = new Array();
        $.each(urls.split(","),function (i,val) {
            urlarr.push({"src":val});
        });
        layer.photos({
            photos: {
                "title": "查看照片" //相册标题
                ,"data": urlarr
            }
            ,shade: 0.01
            ,closeBtn: 1
            ,anim: 5
        });
    };

    /* —————————————————————————————————————————————————————————————————— 订单 ————————————————————————————————————————————————— */

    table.render({
        elem: '#LAY-order-manage'
        , url: '/admin/shop/getorderlist'
        , cols: [[
            {field: 'id', width: 70, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'order_no', title: '订单号', width: 250}
            , {field: 'uid', title: '买家ID', width: 150}
            , {field: 'goodsid', title: '商品ID', width: 110}
            , {field: 'goods_title', title: '商品名称', minWidth: 110}
            , {field: '', title: '商品ID', width: 60, templet: function (d) {
                    return '<img style="display: inline-block; width: auto; height: 100%;" src= "'+ d.goods_thumb +'" layadmin-event="photosPreview" value="'+d.goods_thumb+'">'
                }}
            , {field: 'price', title: '单价', width: 110}
            , {field: 'count', title: '数量', width: 110}
            , {field: 'total_price', title: '总价', width: 110}
            , {field: 'create_time', width: 170, title: '下单时间'}
            , {field: 'express_no', width: 170, title: '快递单号'}
            , {field: '', width: 100, title: '状态', templet: function (d) {
                    switch (d.status) {
                        case 1:
                            return '<span class="layui-badge layui-bg-green">待发货</span>';
                        case 2:
                            return '<span class="layui-badge layui-bg-black">已发货</span>';
                        case 3:
                            return '<span class="layui-badge layui-bg-red">订单完成</span>';
                        default:
                            return "";
                    }
                }}
            , {title: '操作', width: 100, align: 'center', fixed: 'right', toolbar: '#table-orderlist-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-order-manage)', function(obj){
        if(obj.event === 'operation'){
            var index = layer.open({
                type: 2
                ,title: '发货'
                ,content: '/admin/shop/order_dev?id='+obj.data.id
                ,maxmin: true
                ,area: ['500px', '400px']
            });
            layer.full(index);
        }
    });

    exports('shop', {})
});